package com.car.carsharing.service.impl;

import com.car.carsharing.dto.TableListDto;
import com.car.carsharing.mapper.CarMapper;
import com.car.carsharing.pojo.Car;
import com.car.carsharing.service.CarService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Override
    public TableListDto getAllCars(Map map ) {

        Integer pageSize = (Integer) map.get("pageSize");

        if(pageSize == null || "".equals(pageSize)){
            pageSize = 10;
        }
        Integer pageNum = (Integer) map.get("pageNum");
        if(pageNum == null || "".equals(pageNum)){
            pageNum = 1;
        }

        Car car = new Car();
        car.setLicensePlate((String) map.get("licensePlate"));
        car.setType((String) map.get("type"));

        PageHelper.startPage(pageNum,pageSize);
        List<Car> allCars = carMapper.getAllCars(car);
        PageInfo pageInfo = new PageInfo(allCars);
        return new TableListDto().sucess(allCars,"查询成功",pageInfo.getTotal());
    }

    @Override
    public Car getCarById(Integer carId) {
        return carMapper.getCarById(carId);
    }

    @Override
    public int addCar(Car car) {
        return carMapper.addCar(car);
    }

    @Override
    public int updateCar(Car car) {
        return carMapper.updateCar(car);
    }

    @Override
    public List<Car> getCarNum() {
        return carMapper.getCarNuM();
    }
}
